﻿
using System;

using Irony.Compiler;

namespace IronLua.Ast {

	public static class AstExtensions {

		public static TNode To<TNode> (this AstNode node) where TNode : LuaNode
		{
			return (TNode) node;
		}

		public static Statement ToStatement (this AstNode node)
		{
			var statement = node as Statement;
			if (statement != null)
				return statement;

			var expression = node as Expression;
			if (expression != null)
				return new ExpressionStatement (expression);

			throw new NotSupportedException ("Node not convertible to Statement");
		}

		public static Expression ToExpression (this AstNode node)
		{
			var expression = node as Expression;
			if (expression != null)
				return expression;

			if (node.IsName ())
				return new VariableExpression (node);

			throw new NotSupportedException ("Node not convertible to Expression");
		}

		static bool IsName (this AstNode node)
		{
			return node.Element.Name == "Name";
		}
	}
}
